<?php

/**
* TwitterTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';
require_once("Services/Twitter.php");
require_once("HTTP/OAuth/Consumer.php");

class  TwitterTest extends WebTestCase
{

    private $dbh = '';
    private $username = '';
    private $password = '';


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";
        $this->msg = md5( uniqid( rand(), true ) );

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $this->dbh = new PDO($options['dsn_string']); 
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");

        $this->get($this->url.'?');
        $this->assertTitle('Wirtualny Przewodnik');

    }

    public function testEmptyTwitter()
    {
        $twitterOpt = &PEAR::getStaticProperty('CONFIG_INI','TWITTER');
        $this->get($this->url.'?cmd=Twitter');
        $this->assertText('Odpowiedzi do @'.$twitterOpt['username']);   
        $this->assertText('Brak wpisów','This works only if there is now twitt.');   

    }

    public function testTwittExists()
    {

        $this->get($this->url.'?cmd=Twitter');
           

        $twitterOpt = &PEAR::getStaticProperty('CONFIG_INI','TWITTER');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL'); 

        $this->assertText('Odpowiedzi do @'.$twitterOpt['username']);

        $twitter = new Services_Twitter();

        $oauth   = new HTTP_OAuth_Consumer($twitterOpt['consumer_key'],
                                           $twitterOpt['consumer_secret'],
                                           $twitterOpt['auth_token'],
                                           $twitterOpt['token_secret']);


        $twitter->setOAuth($oauth);        
        
        if (isSet($global['proxy']))
        {
            $data = split(':',$global['proxy']);
            $request = new HTTP_Request2();
            $request->setConfig('proxy_host',$data[0]);    
            $request->setConfig('proxy_port',$data[1]);    
            $twitter->setRequest($request);
        }

        $response = $twitter->statuses->update('@'.$twitterOpt['username']." ".$this->msg);
        
        $this->get($this->url.'?cmd=Twitter');
        $this->assertText('@'.$twitterOpt['username']." ".$this->msg);   
        
        $response = $twitter->statuses->destroy($response->id);
    }


    public function tearDown() 
    {
      

    }
 

}
?>
